জাভাস্ক্রিপ্ট এ ভেরিয়েবল ডিক্লায়ার করা হয় var কি ওয়ার্ড লিখে
var x; // defines the variable x and assigns to it the special value "undefined" (not to be confused with an undefined value)
var y = 2; // defines the variable y and assigns to it the value 2
এই ভাষায় কমেন্ট লিখা হয় ২টা স্ল্যাশ ব্যাবহার এর মাধ্যমে (//) ।
এর কোন বিল্ড ইন ফাংশান নাই ; রান টাইম ইনভাইরন মেন্ট এটা প্রদান করে থাকে . The ECMAScript এর ৫ম এডিশনে বলা হয়েছে:
… এখানে কোন প্রভিশনের প্রয়োজন নাই । ডাটা স্ফেকিশনের জন্য .
যদিও অধিকাংশের কনসল অবজেক্ট থাকে, যা দ্বারা সে এর আউটপুট দেখতে পারে :
console.log("Hello World!");
জাভাস্ক্রিপ্ট এর একটা সিম্পল ফাংশন:
function factorial(n) {if (n===0||n===1) return 1; //fact0=fact1=1
return n * factorial(n - 1)}
factorial(3); // returns 6
একটা এনোনমাস ফাংশন :
function counter() {
var count = 0;
return function() {return ++count}};
var closure = counter();
closure(); // returns 1
closure(); // returns 2
closure(); // returns 3
function sum() {var x = 0;
for (var i = 0; i < arguments.length; ++i) x += arguments[i];
return x}
sum(1, 2); // returns 3
sum(1, 2, 3); // returns 6
var counter = (function () {
var i = 0; // private property
return { // public methods
get: function () {
alert(i);
},
set: function (value) {
i = value;
},
increment: function () {
alert(++i);
}
};
})(); // module
counter.get(); // shows 0
counter.set(6);
counter.increment(); // shows 7
counter.increment(); // shows 8
আরো একটি জাভাস্ক্রিপ্ট এর উদাহরন দেখুন ঃ
এই সাধারন কোড জাভাস্ক্রিপ্ট এর বিভিন্য উদাহরণ প্রকাশ করে । .
/* Finds the lowest common multiple (LCM) of two numbers */
function LCMCalculator(x, y) { // constructor function
var checkInt = function (x) { // inner function
if (x % 1 !== 0) {
throw new TypeError(x + " is not an integer"); // throw an exception
}
return x;
};
this.a = checkInt(x)
// semicolons ^^^^ are optional, a newline is enough
this.b = checkInt(y);
}
// The prototype of object instances created by a constructor is
// that constructor's "prototype" property.
LCMCalculator.prototype = { // object literal
constructor: LCMCalculator, // when reassigning a prototype, set the constructor property appropriately
gcd: function () { // method that calculates the greatest common divisor
// Euclidean algorithm:
var a = Math.abs(this.a), b = Math.abs(this.b), t;
if (a < b) {
// swap variables
t = b;
b = a;
a = t;
}
while (b !== 0) {
t = b;
b = a % b;
a = t;
}
// Only need to calculate GCD once, so "redefine" this method.
// (Actually not redefinition—it's defined on the instance itself,
// so that this.gcd refers to this "redefinition" instead of LCMCalculator.prototype.gcd.
// Note that this leads to a wrong result if the LCMCalculator object members "a" and/or "b" are altered afterwards.)
// Also, 'gcd' === "gcd", this['gcd'] === this.gcd
this['gcd'] = function () {
return a;
};
return a;
},
// Object property names can be specified by strings delimited by double (") or single (') quotes.
lcm : function () {
// Variable names don't collide with object properties, e.g., |lcm| is not |this.lcm|.
// not using |this.a*this.b| to avoid FP precision issues
var lcm = this.a/this.gcd()*this.b;
// Only need to calculate lcm once, so "redefine" this method.
this.lcm = function () {
return lcm;
};
return lcm;
},
toString: function () {
return "LCMCalculator: a = " + this.a + ", b = " + this.b;
}
};
// একটা আউটপুট ফাংশন; এই প্রয়োগ সুধুমাত্র ওয়েব ব্রাওজারে কাজ করবে ।
function output(x) {
document.body.appendChild(document.createTextNode(x));
document.body.appendChild(document.createElement('br'));
}
// Note: Array's map() and forEach() are defined in JavaScript 1.6.
// They are used here to demonstrate JavaScript's inherent functional nature.
[[25, 55], [21, 56], [22, 58], [28, 56]].map(function (pair) { // array literal + mapping function
return new LCMCalculator(pair[0], pair[1]);
}).sort((a, b) => a.lcm() - b.lcm()) // sort with this comparative function; => is a shorthand form of a function, called "arrow function"
.forEach(printResult);
function printResult(obj) {
output(obj + ", gcd = " + obj.gcd() + ", lcm = " + obj.lcm());
}
নিচের উদাহরনটা ওয়েব ব্রাউজারে প্রদর্শন করবে ।
LCMCalculator: a = 28, b = 56, gcd = 28, lcm = 56
LCMCalculator: a = 21, b = 56, gcd = 7, lcm = 168
LCMCalculator: a = 25, b = 55, gcd = 5, lcm = 275
LCMCalculator: a = 22, b = 58, gcd = 2, lcm = 638
0 Comments